﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPFExcelReport
{
    public partial class UIGPDAT : ViewBase
    {
        public VGPDAT TMV
        {
            get
            {
                return (VGPDAT)MV;
            }
        }
        #region ==BOX===========
        public static readonly DependencyProperty BOXProperty = DependencyProperty.Register("BOX",
            typeof(int), typeof(UIGPDAT),
            new FrameworkPropertyMetadata(0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnBOXChanged));
        public int BOX
        {
            get
            {
                return (int)GetValue(BOXProperty);
            }
            set
            {
                SetValue(BOXProperty, value);
            }
        }
        private static void OnBOXChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.BOX = (int)e.NewValue;
            }
        }
        #endregion
        #region ==QTY===========
        public static readonly DependencyProperty QTYProperty = DependencyProperty.Register("QTY",
            typeof(double), typeof(UIGPDAT),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQTYChanged));
        public double QTY
        {
            get
            {
                return (double)GetValue(QTYProperty);
            }
            set
            {
                SetValue(QTYProperty, value);
            }
        }
        private static void OnQTYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.QTY = (double)e.NewValue;
            }
        }
        #endregion
        #region ==MU===========
        public static readonly DependencyProperty MUProperty = DependencyProperty.Register("MU",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnMUChanged));
        public string MU
        {
            get
            {
                return (string)GetValue(MUProperty);
            }
            set
            {
                SetValue(MUProperty, value);
                OnMUChg();
            }
        }
        private static void OnMUChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.MU = (string)e.NewValue;
            }
        }
        public virtual void OnMUChg()
        { }
        #endregion
        #region ==QPTYP===========
        public static readonly DependencyProperty QPTYPProperty = DependencyProperty.Register("QPTYP",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPTYPChanged));
        public string QPTYP
        {
            get
            {
                return (string)GetValue(QPTYPProperty);
            }
            set
            {
                SetValue(QPTYPProperty, value);
                TMV.QPTYP = value;
            }
        }
        private static void OnQPTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.QPTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKRS===========
        public static readonly DependencyProperty QPKRSProperty = DependencyProperty.Register("QPKRS",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKRSChanged));
        public string QPKRS
        {
            get
            {
                return (string)GetValue(QPKRSProperty);
            }
            set
            {
                SetValue(QPKRSProperty, value);
                TMV.QPKRS = value;
            }
        }
        private static void OnQPKRSChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.QPKRS = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GSART===========
        public static readonly DependencyProperty GSARTProperty = DependencyProperty.Register("GSART",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGSARTChanged));
        public string GSART
        {
            get
            {
                return (string)GetValue(GSARTProperty);
            }
            set
            {
                SetValue(GSARTProperty, value);
                TMV.GSART = value;
            }
        }
        private static void OnGSARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.GSART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GPSPPART===========
        public static readonly DependencyProperty GPSPPARTProperty = DependencyProperty.Register("GPSPPART",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGPSPPARTChanged));
        public string GPSPPART
        {
            get
            {
                return (string)GetValue(GPSPPARTProperty);
            }
            set
            {
                SetValue(GPSPPARTProperty, value);
                TMV.GPSPPART = value;
            }
        }
        private static void OnGPSPPARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.GPSPPART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GPFBPART===========
        public static readonly DependencyProperty GPFBPARTProperty = DependencyProperty.Register("GPFBPART",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGPFBPARTChanged));
        public string GPFBPART
        {
            get
            {
                return (string)GetValue(GPFBPARTProperty);
            }
            set
            {
                SetValue(GPFBPARTProperty, value);
                TMV.GPFBPART = value;
            }
        }
        private static void OnGPFBPARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.GPFBPART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GPZWPART===========
        public static readonly DependencyProperty GPZWPARTProperty = DependencyProperty.Register("GPZWPART",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGPZWPARTChanged));
        public string GPZWPART
        {
            get
            {
                return (string)GetValue(GPZWPARTProperty);
            }
            set
            {
                SetValue(GPZWPARTProperty, value);
                TMV.GPZWPART = value;
            }
        }
        private static void OnGPZWPARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.GPZWPART = (string)e.NewValue;
            }
        }
        #endregion

        #region ==GPPARTIE===========
        public static readonly DependencyProperty GPPARTIEProperty = DependencyProperty.Register("GPPARTIE",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnGPPARTIEChanged));
        public string GPPARTIE
        {
            get
            {
                return (string)GetValue(GPPARTIEProperty);
            }
            set
            {
                SetValue(GPPARTIEProperty, value);
                TMV.GPPARTIE = value;
            }
        }
        private static void OnGPPARTIEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.GPPARTIE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==PARTIE===========
        public static readonly DependencyProperty PARTIEProperty = DependencyProperty.Register("PARTIE",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnPARTIEChanged));
        public string PARTIE
        {
            get
            {
                return (string)GetValue(PARTIEProperty);
            }
            set
            {
                SetValue(PARTIEProperty, value);
                TMV.PARTIE = value;
            }
        }
        private static void OnPARTIEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.PARTIE = (string)e.NewValue;
            }
        }
        #endregion

        #region ==dwGP===========
        public static readonly DependencyProperty dwGPProperty = DependencyProperty.Register("dwGP",
            typeof(DataRow), typeof(UIGPDAT),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OdwGPChanged));
        public DataRow dwGP
        {
            get
            {
                return (DataRow)GetValue(dwGPProperty);
            }
            set
            {
                SetValue(dwGPProperty, value);
                if (MV == null) return;
                TMV.dwGP = value;
            }
        }
        private static void OdwGPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                DataRow row = (DataRow)e.NewValue;
                src.dwGP = row;
            }

        }
        #endregion

        #region ==Checked===========
        public static readonly DependencyProperty CheckedProperty = DependencyProperty.Register("Checked",
            typeof(bool), typeof(UIGPDAT),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnCheckedChanged));
        public bool Checked
        {
            get
            {
                return (bool)GetValue(CheckedProperty);
            }
            set
            {
                SetValue(CheckedProperty, value);
            }
        }
        private static void OnCheckedChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.Checked = (bool)e.NewValue;
            }
        }
        #endregion

        #region ==QCDOCTYP===========
        public static readonly DependencyProperty QCDOCTYPProperty = DependencyProperty.Register("QCDOCTYP",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQCDOCTYPChanged));
        public string QCDOCTYP
        {
            get
            {
                return (string)GetValue(QCDOCTYPProperty);
            }
            set
            {
                SetValue(QCDOCTYPProperty, value);
                if (MV == null)
                    return;
                TMV.QCDOCTYP = value;
            }
        }
        private static void OnQCDOCTYPChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.QCDOCTYP = (string)e.NewValue;
            }
        }
        #endregion

        #region ==QPKEY===========
        public static readonly DependencyProperty QPKEYProperty = DependencyProperty.Register("QPKEY",
            typeof(string), typeof(UIGPDAT),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnQPKEYChanged));
        public string QPKEY
        {
            get
            {
                return (string)GetValue(QPKEYProperty);
            }
            set
            {
                SetValue(QPKEYProperty, value);

            }
        }
        private static void OnQPKEYChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as UIGPDAT;
            if (src != null)
            {
                src.QPKEY = (string)e.NewValue;
            }
        }
        #endregion

         


        public UIGPDAT()
        {
          
            MESSAGES += "QCRELOAD;REFRESHBYGB33;"; 
        }

        public TDRAUFTR cDR
        {
            get
            {
                return (TDRAUFTR)this.FindName("cDR");
            }
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            base.Window_Loaded(sender, e);
        }

        public override void OnWorkflowIDChange(ReflectionStudio.Core.Events.MessageEventArgs e)
        {
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "QCRELOAD":
                    TMV.QCRELOAD(msg);
                    break;
                case "REFRESHBYGB33":
                    TMV.REFRESHBYGB33(msg);
                    break;

            }
        }
        public override void Query_KeyUp(object sender, KeyEventArgs e)
        {
            TextBox tb = sender as TextBox;
            if (tb.Text.Trim() == "X")
            {
                tb.Focus();
                return;
            }  
            base.Query_KeyUp(sender, e);
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            //GPPARTIE GPGART GPGFARBE GPBEM1 GPBEM2
            dwData = TBMain.NewRow();
            dwData["GPMANDANT"] = app.Mandant;
            dwData["GPGART"] = GSART;
            /* dwData["GPGFARBE"] = FARBE;
            dwData["GPPARTIE"] = GPPARTIE;
            if (GPPARTIE.Trim() == "00000000")
            {
                dwData["GPPARTIE"] = "0";
            }*/
            dwData["GPGFARBE"] = FARBE.Trim();
            dwData["GPSPPART"] = GPSPPART;
            dwData["GPFBPART"] = GPFBPART;
            dwData["GPAENDZL"] = "0";
            dwData["GPFILLER"] = VQPMERKM.Instance.TTLQT(); 
            if (Checked)
            {
                dwData["GPNEUBEN"] = DTSTART;
                dwData["GPAENDBEN"] = DTEND;
                dwData["GPGEPRUEFT"] = "J";
                SResult = TMV.QueryGPDAT(dwData, 12); 
            }
            else
            {
                dwData["GPGEPRUEFT"] = " ";
                SResult = TMV.QueryGPDAT(dwData, 3); 
            } 
            MergeData(SResult, false);
        }

        public void PrintCommand(object sender, RoutedEventArgs e)
        {
            try
            {

                cDR.DocType = QCDOCTYP;
                cDR.PRINT(MV.SetProd);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }


        public void LOADGP()
        {
            string LastOrder = "X";
            MessageInfo msg = new MessageInfo();
            msg.MSGTYPE = "LOADGP";
            msg.Sender = this.RUNID;
            msg.ACTION = "LOADGP";
            if (dwGP == null)
            {
                msg.P2 = "";
                msg.ORDER = "";
            }
            else
            {
                msg.ORDER = dwGP["QOKORDER"].ToString();
                msg.PARTIE = dwGP["GPPARTIE"].ToString();
                msg.P5 = "G";
                msg.ART = dwGP["GPGART"].ToString();
                msg.VAR = dwGP["GPGVAR"].ToString();
                msg.FARBE = dwGP["GPGFARBE"].ToString();
                msg.DESS = "".PadRight(10);
                msg.COLORIT = "".PadRight(10);
                msg.BESCHICH = "".PadRight(10);
                msg.GROE = "".PadRight(7);//GROE
                msg.P1 = "0";
                DataSet Set = new DataSet();
                DataTable TB = TBMain.Clone();
                dwData = TB.Rows.Add();
                dwData.ItemArray = dwGP.ItemArray;
                Set.Merge(TB);
                msg.Set = Set;
            }
            if (LastOrder == msg.ORDER)
                return;
            LastOrder = msg.ORDER;
            VICC_WFEVENTS.SVWF.CheckEvents("UIMESSAGE", null, "", msg);
        }

        public override void EditCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
            {
                MV.TBTemp = TBMain.Copy();
            }
            else
            {
                MV.TBTemp = null;
            }
            base.EditCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            TMV.BATCHGBCHG();
            base.SaveCommand(sender, e);
        }

       
    }
}
